package com.jky.stmall.manger.mapper;

import com.jky.stmall.manger.entity.Goods;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper //Mybatis注解 当前的接口是Mybatis的接口。相当于Application添加MapperScan
public interface GoodsMapper {

    @Insert({"insert into goods",
            "(name,dscp,price,mark_price,color,version,count,content,recom,category_id,status)",
            "value(#{name},#{dscp},#{price},#{markPrice},#{color},#{version},#{count},#{content},#{recom},#{categoryId},#{status})",
    })
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
//插入时获取自增表的主键
    int insert(Goods goods);

    //根据id删除
    @Delete("delete from goods where id=#{id}")
    int delete(Integer id);

    @Update({
            "<script>",
            "update goods",
            "<set>",
            "<if test='name!=null and name.length>0'>name=#{name},</if>",
            "<if test='dscp!=null and dscp.length>0'>dscp=#{dscp},</if>",
            "<if test='price!=null'>price=#{price},</if>",
            "<if test='markPrice!=null'>mark_price=#{markPrice},</if>",
            "<if test='color!=null and color.length>0'>color=#{color},</if>",
            "<if test='version!=null and version.length>0'>version=#{version},</if>",
            "<if test='count!=null'>count=#{count},</if>",
            "<if test='content!=null and content.length>0'>content=#{content},</if>",
            "<if test='recom!=null'>recom=#{recom},</if>",
            "<if test='categoryId!=null'>category_id=#{categoryId},</if>",
            "<if test='status!=null'>status=#{status}</if>",
            "</set>",
            "where id =#{id}",
            "</script>"
    })
    int update(Goods goods);

    @Select("select * from goods where id=#{id}")
    @Results({
            @Result(column = "id", property = "id", id = true),//商品的信息
            @Result(column = "id", property = "pics",
                    many = @Many(select = "com.jky.stmall.manger.mapper.GoodsPicMapper.selectByGoodsId"))//一对多的关联

    })
    Goods selectById(Integer id);

    @Select({
            "<script>",
            "select * from goods",
            "<where>",
            "<if test='name!=null and name.length>0'>name like '%${name}%'</if>",
            "<if test='price!=null'> and price=#{price}</if>",
            "<if test='markPrice!=null'>and mark_price=#{markPrice}</if>",
            "<if test='color!=null'>and color=#{color}</if>",
            "<if test='version!=null'>and version=#{version}</if>",
            "<if test='count!=null'>and count=#{count}</if>",
            "<if test='recom!=null'>and recom=#{recom}</if>",
            "<if test='categoryId!=null and categoryId!=0'>and category_id=#{categoryId}</if>",
            "<if test='categoryId!=null and categoryId==0'>and category_id is null</if>",
            "<if test='status!=null'>and status=#{status}</if>",
            "</where>",
            "</script>"

    })
    @Results(//一个字段的映射 相当于 <id><result>
//关联查询 两个表 连接起来  通过 查询 将查询到 的id传给category1,,column代表将该属性作为参数传入,property代表将查询出来的数据交给该属性
            {       /*@Result(column = "category_id", property = "categoryId"),*/
                    @Result(column = "category_id", property = "category1",
                            one = @One(select = "com.jky.stmall.manger.mapper.CategoryMapper.selectById")),
                    @Result(column = "id", property = "id", id = true),//商品的信息
                    @Result(column = "id", property = "pics",
                            many = @Many(select = "com.jky.stmall.manger.mapper.GoodsPicMapper.selectByGoodsId"))//一对多的关联
            }
    )
    List<Goods> select(Goods goods);
}
